package com.kbbywyclient.ui.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.MinePartnerData;
import com.kbbywyclient.ui.adapter.base.BaseAdapter;
import com.kbbywyclient.util.ImageLoaderUtils;

import java.util.List;
import java.util.Locale;

/**
 * Created by Jiangzx on 2017/10/30 16:53.
 */

public class MinePartnerSummaryAdapter extends BaseAdapter<MinePartnerData.Content.Partner> {
    private int role;
    public MinePartnerSummaryAdapter(int role,List<MinePartnerData.Content.Partner> data) {
        super(data, R.layout.item_mine_partner_summary);
        this.role=role;
    }

    @Override
    public void convert(Holder holder, MinePartnerData.Content.Partner partner, int position) {
        ImageLoaderUtils.displayImageDefaultRes(holder.getView(R.id.iv_avatar),
                API.PIC_ADDRESS+partner.getLogo(),
                R.mipmap.icon_head_portrait);
        holder.setText(R.id.tv_partner_name,partner.getTrueName());
        TextView tvLevel=holder.getView(R.id.tv_partner_level);
        if(role== Constants.LEVEL_DISTRIBUTOR){
            //总代理
            String roleText=partner.getRole();
            tvLevel.setText(roleText);
            if(roleText.contains("一级")){
                tvLevel.setBackgroundResource(R.drawable.shape_corner_2dp_solid_b1e7b3);
            }else if(roleText.contains("二级")){
                tvLevel.setBackgroundResource(R.drawable.shape_corner_2dp_solid_b9ddff);
            }
            holder.setText(R.id.tv_month_application,"本月进件数："+partner.getP_up());
            holder.setText(R.id.tv_month_settlement,"本月结算数："+partner.getP_down());
        }else{
            tvLevel.setVisibility(View.GONE);
            TextView tvLeft=holder.getView(R.id.tv_month_application);
            TextView tvRight=holder.getView(R.id.tv_month_settlement);
            if(role==Constants.LEVEL_NORMAL){
                //普通合伙人
                tvRight.setVisibility(View.GONE);
                tvLeft.setText("本月结算数："+partner.getP_up());
            }else{
                //一级、二级合伙人
                tvLeft.setText("本月结算数："+partner.getP_up());
                String reward=partner.getP_down();
                if(TextUtils.isEmpty(reward)){
                    tvRight.setText("本月奖金：0.00");
                }else{
                    tvRight.setText("本月奖金："+
                            String.format(Locale.CHINESE,
                                    Constants.AMOUNT_FORMAT,
                                    Double.parseDouble(reward)));
                }
            }
        }

        holder.itemView.setOnClickListener(v->{
            if(listener!=null){
                listener.action(ItemClickListener.ACTION_VIEW_DETAIL,partner);
            }
        });

        holder.getView(R.id.iv_partner_call).setOnClickListener(v->{
            if(listener!=null){
                listener.action(ItemClickListener.ACTION_CALL,partner);
            }
        });
    }


    private ItemClickListener<MinePartnerData.Content.Partner> listener;

    public void setItemClickedListener(ItemClickListener<MinePartnerData.Content.Partner> listener){
        this.listener=listener;
    }

    public interface ItemClickListener<T>{
        int ACTION_CALL=1;
        int ACTION_VIEW_DETAIL=2;
        void action(int action,T t);
    }
}
